package jevon.philautus.service.impl;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import jevon.philautus.domain.User;
import jevon.philautus.service.UserService;

import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
	@PersistenceContext
	EntityManager em;

	@Override
	public void save(User user) {
		user.setPasswd(new Sha256Hash(user.getPasswd()).toHex());
		em.persist(user);
	}

	@Override
	public User findUser(String username) {
		Query query = em.createQuery("from User u where u.username=:username");
		query.setParameter("username", username);
		return (User) query.getSingleResult();
	}

	@Override
	public User getUser(Long userId) {
		return em.find(User.class, userId);
	}

}
